//导入main文件中的后端链接
import {baseUrl} from '../main.js';

const request = (url, data) => {
	return new Promise((resolve, reject) => {
		
		//取出缓存中的token登录令牌
		const userId = uni.getStorageSync('userId');
		const token = uni.getStorageSync('token');
		
		uni.request({
			method: "POST",
			url: baseUrl + url,
			data: data,
			header: {
				"userId": userId,
				"token": token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			dataType: 'json',
		}).then((response) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 1000);
			let [error, res] = response;
			
			//如果没有登录令牌
			if(res.data.code == 440){
				uni.showToast({
				    title: '请先登录',
					icon: 'none',
				    duration: 2000
				});
				//返回登录页面
				uni.navigateTo({
					url: '/pages/login/login',
				});
			}
			resolve(res.data);
		}).catch(error => {
			let [err, res] = error;
			reject(err)
		})
	});
}
export default request
